//
//  DealDetailViewController.m
//  小团购
//
//  Created by 樊荣海 on 15/2/3.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "DealDetailViewController.h"

@interface DealDetailViewController ()

@end

@implementation DealDetailViewController {
    
    
    BuyView *_buyView;//上方购买
    
    DetailDock *_detailView;//侧边Dock
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = globalBgColor;
    self.navigationItem.title =_dealModel.title;
    
    // 右上角的按钮
    NSString *collectIcon = [[DBTool sharedDBTool] dealIsExist:_dealModel] ? @"ic_collect_suc.png" : @"ic_deal_collect.png";
    self.navigationItem.rightBarButtonItems = @[
                                                [UIBarButtonItem itemWithIcon:@"btn_share.png" highlightedIcon:@"btn_share_pressed.png" target:nil action:nil],
                                                [UIBarButtonItem itemWithIcon:collectIcon highlightedIcon:@"ic_deal_collect_pressed.png" target:self action:@selector(collect)]];
    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectChange) name:kCollectChangeNote object:nil];
    
    _buyView = [BuyView buyView];
    _buyView.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);
    _buyView.dealModel = _dealModel;
    [self.view addSubview:_buyView];
    
    _detailView = [DetailDock detailDock];
    _detailView.delegate = self;
    _detailView.frame = CGRectMake(self.view.frame.size.width-45, (self.view.frame.size.height-60-425)/2, 0, 0);
    [self.view addSubview:_detailView];
    
    //子控制器
    InfoViewController *info = [[InfoViewController alloc] init];
    info.dealModel = _dealModel;
    
    WebViewController *web = [[WebViewController alloc] init];
    web.dealModel = _dealModel;
    
    DetailViewController *detail = [[DetailViewController alloc] init];
    
    [self addChildViewController:info];
    [self addChildViewController:web];
    [self addChildViewController:detail];
    
    [self detailDock:_detailView selectedFrom:0 to:0];
}

- (void)detailDock:(DetailDock *)detailDock selectedFrom:(NSInteger)from to:(NSInteger)to {
    
    UIViewController *oldVC = self.childViewControllers[from];
    UIViewController *newVC = self.childViewControllers[to];
    
    [oldVC.view removeFromSuperview];
    
    //[重要]设置新控制器view的frame
    newVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width -_detailView.frame.size.width, self.view.frame.size.height);
    newVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self.view insertSubview:newVC.view atIndex:0];
}




#pragma mark 收藏
- (void)collect
{
    if ([[DBTool sharedDBTool] dealIsExist:_dealModel]) { // 如果存在--删掉
        [[DBTool sharedDBTool] deleteDealModel:_dealModel];
    } else { // 不存在--收藏
        [[DBTool sharedDBTool] insertDealModel:_dealModel];
    }
    
    // 发出通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kCollectChangeNote object:nil];
}

#pragma mark 收藏状态改变
- (void)collectChange
{
    UIButton *btn = (UIButton *)[self.navigationItem.rightBarButtonItems[1] customView];
    if ([[DBTool sharedDBTool] dealIsExist:_dealModel]) {
        [btn setBackgroundImage:[UIImage imageNamed:@"ic_collect_suc.png"] forState:UIControlStateNormal];
    } else {
        [btn setBackgroundImage:[UIImage imageNamed:@"ic_deal_collect.png"] forState:UIControlStateNormal];
    }
}

@end
